// pages/profile/Profile.js
var network = require("../../api/api.js")
var basicData = require('../../utils/data')
var util = require('../../utils/util')
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkColor: '#52a4f8',
      checkedColor: '#ffffff',
      questionnaireData: {
          own_opinion: ''
      },
      selectData: {
      },
      estimate_table_disabled: false,
      estimate_frequency_disabled: false,
    select1: [
        {name: '从不', value: '1', checked: false},
        {name: '偶尔', value: '2', checked: false},
        {name: '时常', value: '3', checked: false}
    ],
      select2: [
          {name: '是', value: '1', checked: false},
          {name: '否', value: '2', checked: false}
      ],
      select3: [
          {name: '从不', value: '1', checked: false},
          {name: '偶尔', value: '2', checked: false},
          {name: '视情况而行', value: '3', checked: false},
          {name: '常规', value: '4', checked: false},
      ],
      select4: [
          {name: '完全不知道', value: '1', checked: false},
          {name: '知道一点，但不确切', value: '2', checked: false},
          {name: '比较清楚', value: '3', checked: false},
          {name: '非常清楚', value: '4', checked: false},
      ],
      select5: [
          {name: '在校学习', value: '1', checked: false},
          {name: '学术会议、讲座', value: '2', checked: false},
          {name: '谵妄相关知识学习班', value: '3', checked: false},
          {name: '因个人兴趣或工作需要自学', value: '4', checked: false},
          {name: '工作经验积累', value: '5', checked: false},
          {name: '同事之间交流', value: '6', checked: false},
          {name: '科室小讲课或读书报告', value: '7', checked: false},
          {name: '请教相关专家', value: '8', checked: false},
          {name: '谵妄相关知识宣传小册子', value: '9', checked: false},
          {name: '相关媒体报道', value: '10', checked: false},
          {name: '其它', value: '11', checked: false}
      ],
      select6: [
          {name: '谵妄的定义和分类', value: '1', checked: false},
          {name: '危险因素和病因', value: '2', checked: false},
          {name: '谵妄的评估和诊断（如专业量表的使用', value: '3', checked: false},
          {name: '预防和处理', value: '4', checked: false},
          {name: '其它', value: '5', checked: false}
      ],
      select7: [
          {name: '100%~80% ', value: '1', checked: false},
          {name: '80%~60%', value: '2', checked: false},
          {name: '60%~40% ', value: '3', checked: false},
          {name: '40%-20% ', value: '4', checked: false},
          {name: '20%', value: '5', checked: false},
          {name: '0%', value: '6', checked: false}
      ],
      select8: [
          {name: '非常有必要', value: '1', checked: false},
          {name: '有必要', value: '2', checked: false},
          {name: '没有必要', value: '3', checked: false},
          {name: '无所谓', value: '4', checked: false}
      ],
      select9: [
          {name: '谵妄知识不足', value: '1', checked: false},
          {name: '人力资源配置不合理，工作太忙，时间又有限，缺乏和患者沟通', value: '2', checked: false},
          {name: '老年谵妄多表现为抑制型，其夜间多发，不易被发现', value: '3', checked: false},
          {name: '缺乏简易、精准的评估工具', value: '4', checked: false},
          {name: '缺乏系统的谵妄观察评估流程、常规筛查和高危因素评估', value: '5', checked: false},
          {name: '医护间合作不足', value: '6', checked: false},
          {name: '认为谵妄是一过性的，无需重视', value: '7', checked: false},
          {name: '其它', value: '8', checked: false}
      ],
      select10: [
          {name: '专业知识和技能缺乏', value: '1', checked: false},
          {name: '本身工作量大、节奏快，工作时间有限', value: '2', checked: false},
          {name: '增加了医生、护士的工作量', value: '3', checked: false},
          {name: '缺乏行政领导的大力支持，工作很难开展', value: '4', checked: false},
          {name: '缺乏有效的沟通途径和完善的激励机制', value: '5', checked: false},
          {name: '缺乏相应的团队工作氛围', value: '6', checked: false},
          {name: '其它', value: '7', checked: false}
      ]
  },
    deliration_estimate_input: function (e) {
        var value = e.detail.value
        if (value === '1') {
            this.setData({
                questionnaireData: Object.assign({}, this.data.questionnaireData, {estimate_table: '', estimate_frequency: ''}),
                estimate_table_disabled: true,
                estimate_frequency_disabled: true
            })
        } else {
            this.setData({
                estimate_table_disabled: false,
                estimate_frequency_disabled: false
            })
        }
    },
    questionnaireInput: function (e) {
      console.log(e)
        console.log(e.target.dataset)
        console.log(e.detail.value)
        const key = e.target.dataset.key
        const data = new Object()
        data[key] = e.detail.value
        if (this.data.selectData[key]) {
          let select_data = this.data.selectData[key]
            select_data = select_data.map( v => {
                return {
                   name: v.name,
                   value: v.value,
                   checked:  v.value === e.detail.value
                }
            })
            console.log(select_data)
            this.data.selectData[key] = select_data
            console.log(this.data.selectData)
        }
        this.setData({
            questionnaireData: Object.assign({}, this.data.questionnaireData, data),
            selectData: this.data.selectData
        })
    },
    questionnaireCheckbox: function (e) {
        const key = e.target.dataset.key
        const data = new Object()
        data[key] = e.detail.value.join()
        if (this.data.selectData[key]) {
            let select_data = this.data.selectData[key]
            select_data = select_data.map( v => {
                return {
                    name: v.name,
                    value: v.value,
                    checked:  e.detail.value.indexOf(v.value.toString()) >= 0
                }
            })
            console.log(select_data)
            this.data.selectData[key] = select_data
            console.log(this.data.selectData)
        }
        this.setData({
            questionnaireData: Object.assign({}, this.data.questionnaireData, data),
            selectData: this.data.selectData
        })
    },
    saveQuestionnaire: function () {
        var that = this
        var userLogin = wx.getStorageSync('user_login') || {}
        var params = new Object()
        params.business_param= this.data.questionnaireData
        params.business_param.user_id = String(userLogin.user_id)
        network.POST(
            {
                params: params,
                url: "investigate_questionnaire",
                success: function (res) {
                  if (res.result_state == "0"){
                    //show_invest    1: 显示 0： 不显示
                    wx.setStorage({
                      key: "show_invest",
                      data: "0"
                    })
                  }
                        wx.navigateBack({
                            delta: 1
                        })
                },
                fail: function () {
                    //失败后的逻辑

                },
            })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.data.selectData.deliration_cognition_estimate = this.data.select1
      this.data.selectData.deliration_estimate = this.data.select1
      this.data.selectData.prevent_status = this.data.select2
      this.data.selectData.doctor_deliration_record_status = this.data.select3
      this.data.selectData.nursedeliration_record_status = this.data.select3
      this.data.selectData.synthesize_intervene = this.data.select2
      this.data.selectData.deliration_definition = this.data.select4
      this.data.selectData.deliration_harm = this.data.select4
      this.data.selectData.deliration_cause = this.data.select4
      this.data.selectData.deliration_diagnose = this.data.select4
      this.data.selectData.deliration_expression = this.data.select4
      this.data.selectData.deliration_handle = this.data.select4
      this.data.selectData.study_deliration_status = this.data.select2
      this.data.selectData.train_deliration_status = this.data.select2
      this.data.selectData.study_status = this.data.select2
      this.data.selectData.work_train_deliration_status = this.data.select2
      this.data.selectData.ability_to_possess = this.data.select2
      this.data.selectData.understand_screening_table = this.data.select2
      this.data.selectData.understand_screening_table = this.data.select2
      this.data.selectData.screening_table_find_status = this.data.select2
      this.data.selectData.treatment_of_patients = this.data.select2
      this.data.selectData.non_drug_therapy_status = this.data.select2
      this.data.selectData.lack_of_training = this.data.select2
      this.data.selectData.estimate_rate = this.data.select7
      this.data.selectData.assessment_of_necessity = this.data.select8
      this.data.selectData.prevent_of_necessity = this.data.select8
      this.data.selectData.record_of_necessity = this.data.select8
      this.data.selectData.quality_combination_of_necessity = this.data.select8
      this.data.selectData.work_study_of_necessity = this.data.select8
      this.data.selectData.synthesize_of_necessity = this.data.select8
      this.data.selectData.follow_up_of_necessity = this.data.select8
      this.data.selectData.deliration_knowledge_source = this.data.select5
      this.data.selectData.hope_study = this.data.select6
      this.data.selectData.influencing_factor_of_necessity = this.data.select9
      this.data.selectData.difficulty_item = this.data.select10
      this.setData({
          selectData: this.data.selectData
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})